<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/2/23
 * Time: 10:58
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Beautytipscate extends Base
{
    /**
     * 美丽技巧分类
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $where['btc_is_del'] = 1;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('beauty_tips_cate')->where($where)->count();// 获取总条数
            $lists = Db::name('beauty_tips_cate')
                ->where($where)
                ->field('btc_id, btc_name, btc_time, btc_updatetime')
                ->order('btc_time desc')
                ->page($Nowpage,$limits)
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加分类
     * @return mixed|\think\response\Json
     */
    public function cateAdd(){
        if(Request::instance()->isPost()){
            $btc_name = trim(input('btc_name'), ' ');
            $insert = [
                'btc_name' => $btc_name,
                'btc_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_beauty_tips_cate')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('beautytipscate/cateadd');
    }
    
    /**
     * 编辑分类
     * @return mixed|\think\response\Json
     */
    public function cateEdit(){
        $btc_id = input('btc_id');
        if(Request::instance()->isPost()){
            $btc_name = trim(input('btc_name'), ' ');
            $update = [
                'btc_name' => $btc_name,
                'btc_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_beauty_tips_cate')->where('btc_id', $btc_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $btc = Db::table('up_beauty_tips_cate')->where('btc_id', $btc_id)->field('btc_name')->find();
        return $this->fetch('beautytipscate/cateedit', ['btc' => $btc, 'btc_id' => $btc_id]);
    }
    
    /**
     * 删除分类
     * @return \think\response\Json
     */
    public function cateDel(){
        $btc_id = input('id');
        $res = Db::table('up_beauty_tips_cate')->where('btc_id', $btc_id)->setField('btc_is_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}